//小仓库 关于layout的设置
import { defineStore } from "pinia";
import { reqNoticeType, reqNoticeTypeTotal, reqSearchNoticeType, reqAddNoticeType, reqUpdateNoticeType, reqDeleteNoticeType, reqDeleteNoticeTypes, reqUpdateNoticeStatus } from "@/api/noticeType";
import { ElMessage } from "element-plus";

let useNoticeTypeStore = defineStore("NoticeTypeStore", {
    state: () => {
        return {
            noticeTypeList: [], // 列表
            total: 0, // 总数
        };
    },
    // 异步 | 逻辑的地方
    actions: {
        async getNoticeType(pageNo: number, limit: number) {
            // 获取数据
            let res = await reqNoticeType(pageNo, limit);
            if (res.code == 200) {
                this.noticeTypeList = res.data;
            }
        },
        async getNoticeTypeTotal() {
            let res = await reqNoticeTypeTotal();
            if (res.code == 200) {
                this.total = res.data;
            }
        },
        async searchNoticeType(data: any) {
            var data1 = JSON.stringify(data);
            let res = await reqSearchNoticeType(data1);
            if (res.code == 200) {
                if (res.data.noticeTypeList.length == 0) {
                    ElMessage.error('查询失败')
                    return;
                } else {
                    this.noticeTypeList = res.data.noticeTypeList;
                    this.total = res.data.total;
                    ElMessage.success('查询成功')
                }
            }
        },
        async addNoticeType(data: any) {
            var data1 = JSON.stringify(data);
            let res = await reqAddNoticeType(data1);
            if (res.code == 200) {
                this.noticeTypeList = res.data;
                ElMessage.success('添加成功')
            } else {
                ElMessage.error('添加失败')
            }
        },
        async updateNoticeType(data: any) {
            // 更新
            var data1 = JSON.stringify(data);
            let res = await reqUpdateNoticeType(data1);
            if (res.code == 200) {
                this.noticeTypeList = res.data;
                ElMessage.success('更新成功')
            } else {
                ElMessage.error('更新失败')
            }
        },
        async deleteNoticeType(id: number) {
            // 删除
            let res = await reqDeleteNoticeType(id);
            if (res.code == 200) {
                ElMessage.success('删除成功')
            } else {
                ElMessage.error('删除失败')
            }
        },
        async deleteNoticeTypes(ids: number[]) {
            // 批量删除
            let res = await reqDeleteNoticeTypes(ids);
            if (res.code == 200) {
                ElMessage.success('删除成功')
            } else {
                ElMessage.error('删除失败')
            }
        },
        async changeStatus(id: number, status: boolean) {
            // 封装参数
            let data = {
                id: id,
                status: status
            }
            var data1 = JSON.stringify(data);
            // 修改状态
            let res = await reqUpdateNoticeStatus(data1);
            if (res.code == 200) {
                ElMessage.success('修改成功')
            } else {
                ElMessage.error('修改失败')
            }
        }
    },
    getters: {

    }
});

export default useNoticeTypeStore;